package com.sebiotec.login.ui.activity;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;


import com.allen.library.factory.ApiFactory;
import com.sebiotec.basecommon.utils.constant.SjmConstant;
import com.sebiotec.basecommon.utils.tools.SPUtils;
import com.sebiotec.login.R;
import com.jakewharton.rxbinding2.view.RxView;
import com.xuexiang.xui.widget.toast.XToast;

import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;


public class SettingDialogFragment extends DialogFragment {


    private TextView tvTitle;
    private TextView tvYes;
    private TextView tvNo;
    private String tittle;
    private EditText editText;

    public interface ConfirmListener {
        void onClickComplete(String tittle);
    }
    public static SettingDialogFragment newInstance(String tittle) {
        SettingDialogFragment fragment = new SettingDialogFragment();
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //设置背景透明
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return super.onCreateView(inflater, container, savedInstanceState);


    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
         View view= LayoutInflater.from(getActivity()).inflate(R.layout.dialog_setting, null);
          tvYes=(TextView)view.findViewById(R.id.tv_yes);
          tvNo=(TextView)view.findViewById(R.id.tv_no);
          editText = (EditText) view.findViewById(R.id.et_ip);
        editText.setText( SPUtils.get(SjmConstant.URl,SjmConstant.BASE_URL));
        initListener();

        builder.setView(view);
        return builder.create();

    }


    private void initListener() {
        RxView.clicks(tvYes)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (TextUtils.isEmpty(editText.getText().toString().trim())){

                            XToast.error(getActivity(),getActivity().getString(R.string.service_address)).show();
                        }else{
                            SPUtils.put(SjmConstant.URl,editText.getText().toString().trim());
                            XToast.success(getActivity(),getActivity().getString(R.string.service_address_success)).show();
                            ApiFactory.getInstance().clearAllApi();
                            ApiFactory.getInstance().setBaseUrl(SPUtils.get(SjmConstant.URl,SjmConstant.BASE_URL));
                            dismiss();
                        }


                    }
                });

        RxView.clicks(tvNo)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        dismiss();
                    }
                });
    }

    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();

        getActivity().getWindowManager().getDefaultDisplay().getMetrics( dm );
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.gravity= Gravity.CENTER;

        windowParams.width = (new Double(dm.widthPixels*0.7)).intValue(); //屏幕宽度
        windowParams.height = dm.heightPixels/3; //屏幕高度的1/3
        window.setAttributes(windowParams);

      //
    }
}
